#
# ==============
# Figure 1: IP rates and amounts by agency by year for top 10 outlay agencies.
# ==============
#
  # All improper payment data.
  DT = fread(file.path(DATALOC,"payments_all.csv"))
  DT[,outlaysamountm := as.numeric(outlaysamountm)]
  DT[,improperpaymentamountm := as.numeric(improperpaymentamountm)]

  # Fix 2021 rates: IP rates for 2021 are in percent, while in pre-2021 in proportions.
  DT[,improperpaymentrate := improperpaymentamountm/outlaysamountm]
  # Drop HUD 2021, which is off.
  DT = DT[!(fiscalyear == 2021 & agency == "hud"),]

  # Aggregate to the agency-year and turn to percent.
  AY = DT[,.(ip_rate=100*weighted.mean(x=improperpaymentrate,w=outlaysamountm,na.rm=T),outlays=sum(outlaysamountm,na.rm=T),.N),keyby=c("fiscalyear","agency")]

  # Top 10 total outlays.
  Z = 10
  AY[,tot_outlays := sum(outlays),by="agency"]
  fred = unique(AY[,c("agency","tot_outlays")])
  setkey(fred,tot_outlays)
  PY = merge(AY,fred[seq(.N,.N-Z+1),c("agency")],by="agency")
  PY[agency == "treasury",agency := "DTRS"]
  f = file.path(FIGURELOC,"Figure01-PanelA.pdf")
  pdf(f,width=10,height=6)
  par(mar=c(2.1,8.1,0.1,0.9),cex.lab=1.1,cex.axis=1.1)
  PY[,plot(x=fiscalyear,y=ip_rate,type='n',ann=F,axes=F,log="y",xlim=range(fiscalyear,na.rm=T)-c(1,-1))]
  axis(1,at=seq(2000,2020,2),labels=seq(2000,2020,2),cex.axis=1,family="mono")
  axis(2,las=2,cex.axis=1.1,family="mono")
  mtext("Agency\nimproper\npayment\nrate\n(percent)",side=2,line=8,las=1,adj=0,cex=1.2)
  # Agency line color.
  PY[,agency_id := .GRP,by="agency"]
  PY[,agency_col := agency_id] # gray version: gray(agency_id/10)]
  PY[,agency_lty := ceiling(agency_id/8)]
  PY[,lines(x=fiscalyear,y=ip_rate,lwd=3,col=agency_col,lty=agency_lty),by="agency"]
  # Note agency name.
  PY[,first_year := min(fiscalyear),by="agency"]
  right_axis_labs = c("dod","hhs","usda","dot","opm","DTRS")
  # Left axis.
  PY[fiscalyear == first_year & !agency %in% right_axis_labs,text(x=min(fiscalyear),y=ip_rate,labels=toupper(agency),pos=2,cex=1.2,family="mono",font=2,col=agency_col)]
  # Right axis.
  PY[fiscalyear == 2021 & agency %in% right_axis_labs,text(x=fiscalyear,y=ip_rate,labels=toupper(agency),pos=4,cex=1.2,family="mono",font=2,col=agency_col)]
  dev.off()
  #system2("open",f)

  #
  # IP amounts by agency by year.
  #
  # Top Z total outlays.
  fred = unique(AY[,c("agency","tot_outlays")])
  setkey(fred,tot_outlays)
  PY = merge(AY,fred[seq(.N,.N-Z+1),c("agency")],by="agency")
  PY[agency == "treasury",agency := "DTRS"]
  PY[,ip_amt := (ip_rate/100) * outlays/1000]
  f = file.path(FIGURELOC,"Figure01-PanelB.pdf")
  pdf(f,width=10,height=6)
  par(mar=c(2.1,7.1,0.1,0.9),cex.lab=1.1,cex.axis=1.1)
  PY[,plot(x=fiscalyear,y=ip_amt,type='n',ann=F,axes=F,log="y",xlim=range(fiscalyear,na.rm=T)-c(1,-1))]
  axis(1,at=seq(2000,2020,2),labels=seq(2000,2020,2),cex.axis=1,family="mono")
  axis(2,las=2,at=c(.1,.5,1,5,10,25,50,100),labels=c(.1,.5,1,5,10,25,50,100),cex.axis=1.1,family="mono")
  mtext("Agency\nimproper\npayments\n(Billions $)",side=2,line=7,las=1,adj=0,cex=1.2)
  # Agency line color.
  PY[,agency_id := .GRP,by="agency"]
  PY[,agency_col := agency_id] # gray version: gray(agency_id/10)]
  PY[,agency_lty := ceiling(agency_id/8)]
  PY[,lines(x=fiscalyear,y=ip_amt,lwd=3,col=agency_col,lty=agency_lty),by="agency"]
  # Note agency name.
  PY[,first_year := min(fiscalyear),by="agency"]
  right_axis_labs = c("dol","hhs","usda","dot","opm","DTRS")
  # Left axis.
  PY[fiscalyear == first_year & !agency %in% right_axis_labs,text(x=min(fiscalyear),y=ip_amt,labels=toupper(agency),pos=2,cex=1.2,family="mono",font=2,col=agency_col)]
  # Right axis.
  PY[fiscalyear == 2021 & agency %in% right_axis_labs,text(x=fiscalyear,y=ip_amt,labels=toupper(agency),pos=4,cex=1.2,family="mono",font=2,col=agency_col)]
  dev.off()
